// pages/create/create.js
Page({
  shangchuan(){
    var that = this;
    wx.chooseMedia({
      count: 1,
      success(res) {
          console.log(res.tempFiles)
          var po = res.tempFiles[0].tempFilePath.lastIndexOf(".")
          var ext = res.tempFiles[0].tempFilePath.slice(po)
          wx.cloud.uploadFile({
              cloudPath: that.data.picname + ext,
              filePath: res.tempFiles[0].tempFilePath,
              success(res) {
                  console.log("OKKKK!!!:"+res.fileID)
                  that.data.imageList.push(res.fileID)
                  that.setData({
                    imageList:that.data.imageList
                  });
              },
              fail(err){
                console.log("OKKKKerrr!!!:"+err.errMsg)
              }
          })
      }
  })
  },
  picname:function(e){
    this.setData({
      picname:e.detail.value+'_'+new Date().getTime()
    })
  },
  choose:function(e){
    var k = e.currentTarget.id;
      this.setData({
      flag3:true,
       currentImage:this.data.imageList[k],
       currentIndex:k})
      },
      choose:function(e){
        var k = e.currentTarget.id;
          this.setData({
          flag3:true,
           currentImage:this.data.imageList[k],
           currentIndex:k
          });
       },
       delete(){
            console.log(this.data.currentIndex);
        this.data.imageList.splice(this.data.currentIndex,1);
        this.setData({
           imageList:this.data.imageList,
           flag3:false
        });
        wx.cloud.deleteFile({
          fileList: [this.data.currentImage] // 替换为实际的fileID列表，可以一次删除多个文件
        }).then(res => {
          console.log('删除云存储中的文件成功', res);
          this.setData({
            picname:null,
            currentImage:null,
            currentIndex:0
          });
        }).catch(console.error);
        },   
         create(){
              var that = this;
                var database = wx.cloud.database();
                var data = {
                  title:that.data.matchname,
                  userid:that.data.userid
                };
                database.collection("tb_match").add({data:{
                  title:that.data.matchname,
                  userid:that.data.userid,
                  imageList:that.data.imageList
                }
              }).then((res)=>{
                  console.log("插入一条数据后返回数据库："+res);
                  if(res){
                    wx.showToast({
                      title: '创建比赛成功',
                    })
                  }else{
                    wx.showToast({
                      title: '创建比赛失败',
                    })
                  }
                }).catch((err)=>{
                  console.log(err);
                });
            },
    /**
     * 页面的初始数据
     */
    data: {
      imageList:[],
      picname:null,
      title:null,
      currentImage:null,
      currentIndex:0,
      flag3:false,
       userid:null
    },
   

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      var that = this;
            wx.getStorage({
              key:"userid",
              success(res){
                that.setData({
                  userid:res.data
                }); 
                console.log("加载页面的用户ID：" +res.data);
              }
            })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})
